const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 向服务器发送请求方法
 */
function requestLoading(url, data, method) {
  return new Promise((resolve) => {
    wx.request({
      url: url,
      data: data,
      method: method,
// <<<<<<< HEAD
      // success: function(res) {
      //   if (res.statusCode == 200) {
// =======
      success: function (res) {
        // if (res.statusCode == 200) {
// >>>>>>> e0a2d193f0b844199787b5d2cc8883304753f865
          resolve(res)
        // } else {
        //   fail()
        // }
      },
      error: function(error) {
        resolve(error)
        wx.showModal({
          content: `请求失败，错误码：${error.status}`,
          showCancel: false,
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        });
      }
    })
  })
}

/**
 * 上传身份证图片方法
 */
// function uploadFile(arrName, arrURL, b) {
//   try {
//     var filePath = arrURL[b].toString()
//     console.log("filePath=" + (b + 1) + "次" + filePath)
//     var filename = arrName[b]
//     console.log("filePath=" + (b + 1) + "次" + filePath)
//     var _url = app.globalData.baseUrl + "uploadIDCard"
//     console.log(_url)
//   } catch (e) {
//     if (filePath) {
//       wx.uploadFile({
//         url: _url,
//         filePath: filePath,
//         name: filename,
//         success: function(res) {
//           console.log(res)
//           var data = JSON.parse(res.data)
//           console.log(data)
//         }
//       })
//     }
//   }
// }

/**
 * 无取消的提示方法
 */
function noShowModal(title, content){
  return wx.showModal({
    title: title,
    content: content,
    showCancel: false
  })
}

/**
 * 有取消的提示方法
 */
function isShowModal(title, content,) {
  return wx.showModal({
    title: title,
    content: content,
    confirmText: "确认",
    cancelText: "取消",
    // success: res => {
    //   return success
    // } 
  })
}

module.exports = {
  formatTime: formatTime,
  requestLoading: requestLoading,
  // uploadFile: uploadFile,
  noShowModal: noShowModal
}